//
//  TravelSearchCenterController.swift
//  AirTravel
//
//  Created by apple on 2021/7/28.
//

import UIKit
import Parchment
import HandyJSON
class TravelSearchCenterController: UIViewController {

    var currCityIndex = ""
    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var topLine: NSLayoutConstraint!
    @IBOutlet weak var seachTf: UITextField!
    @IBOutlet weak var addressBtn: UIButton!
    @IBOutlet weak var topView: UIView!
    
    
    var ruleData:CityRuleMianModel?
    var numbser = 0
    var dataSouce = ["全部","跟团游","自由行","邮轮行","私家团","周边游","门票","周边跟团","亲子游学","主题旅游","一日游"]
    
    
    lazy var pagingView: PagingViewController = {
        let pagingViewController = PagingViewController()
        pagingViewController.dataSource = self
        pagingViewController.delegate = self
        pagingViewController.menuItemSize = .selfSizing(estimatedWidth: 60, height: 40)
        pagingViewController.font = UIFont.systemFont(ofSize: 14)
        pagingViewController.selectedFont = UIFont.systemFont(ofSize: 14)
        pagingViewController.selectedTextColor = Green_COLOR
        pagingViewController.indicatorColor = Green_COLOR
        pagingViewController.menuItemLabelSpacing = 20
        pagingViewController.indicatorOptions = .visible(
          height: 2,
          zIndex: Int.max,
          spacing: UIEdgeInsets.init(top: 0, left: 12, bottom: 0, right: 12),
          insets: UIEdgeInsets.zero
        )
        return pagingViewController
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if SCREEN_HEIGHT >= 812 {
            self.topLine.constant = 88
        }else{
            self.topLine.constant = 64
        }
        self.topView.layer.cornerRadius = 20
        self.topView.layer.masksToBounds = true
        
        
        getruleData()
    }
    
    
    func getruleData() {
        
        NetWorkRequest(API1.getScenicSpotCondition) { responds in
            guard let model = JSONDeserializer<CityRuleMianModel>.deserializeFrom(json: "\(responds)") else {
                return
                
            }
            
            self.ruleData = model
            
            self.setUppagingView()
            
        }
        
    }

    func setUppagingView() {
        addChild(self.pagingView)
        self.backView.addSubview(self.pagingView.view)
        self.backView.constrainToEdges(self.pagingView.view)
        self.pagingView.didMove(toParent: self)
        self.pagingView.select(index: self.numbser)
//        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
//            NotificationCenter.default.post(name: NSNotification.Name("ClassificationNotification"), object: nil, userInfo: ["id":self.category_id ,"list":self.sonArray ?? [],"search":self.search])
//        }
    }
    
    @IBAction func backBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
}

extension TravelSearchCenterController: PagingViewControllerDataSource,PagingViewControllerDelegate {
  func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
    let str = "\(self.dataSouce[index])"
    return PagingIndexItem(index: index, title: str)
    
  }
  
  func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
    
    let vc =  TravelSearchListController()
    vc.currCityIndex = currCityIndex
    vc.ruleData = self.ruleData
     return vc
  }
  
  func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
    return self.dataSouce.count
  }
    
    func pagingViewController(
      _ pagingViewController: PagingViewController,
      didScrollToItem pagingItem: PagingItem,
      startingViewController: UIViewController?,
      destinationViewController: UIViewController,
        transitionSuccessful: Bool){
        if transitionSuccessful {
//            let item = pagingItem as! PagingIndexItem
//            if let array = self.leftDatas {
//                let dic = array[item.index] as! Dictionary<String,Any>
//                self.category_id = "\(dic["id"] ?? "")"
//                self.sonArray = dic["son"] as? Array<Any>
//                NotificationCenter.default.post(name: NSNotification.Name("ClassificationNotification"), object: nil, userInfo: ["id":self.category_id,"list":self.sonArray ?? [],"search":self.search])
//            }
//
        }
    }
}
